# -*-* encoding:UTF-8 -*-
# author : mengy
# date   : 2018/11/22

import smtplib
import re
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

email_user = "1215012319@qq.com"
email_pwd = "dishang0117/"

email_host = "smtp.qq.com"
email_port = 25

receivers = ['mengyanyuan@outlook.com','1215012319@qq.com']

message = MIMEMultipart('related')
message['From'] = Header("python 邮件测试","utf-8")
message['To'] = Header("接收者","utf-8")
message['Subject'] = Header("python 邮件测试","utf-8")

msgAlternative = MIMEMultipart("alternative")
message.attach(msgAlternative)

file = open("image.html","rb")
content = file.read()
print content
msgAlternative.attach(MIMEText(content,"html","utf-8"))

img = open("liqin.jpg","rb")
msgImg = MIMEImage(img.read())
img.close()

# 定义图片 ID，在 HTML 文本中引用
msgImg.add_header("Content-ID","<image1>")
message.attach(msgImg)

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(email_host,email_port)
    smtpObj.login(email_user,email_pwd)
    smtpObj.sendmail(email_user,receivers,message.as_string())
    print "邮件发送成功"
except Exception,e:
    print "邮件发送失败:",e

